using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XFGameFramework;

public class SettingPanel : Panel
{
    GameController gameController => Module.LoadController<GameController>();
#region 字段
    [SerializeField] private Text text_volume;
    [SerializeField] private Slider slider_volume;
    [SerializeField] private Text text_sound;
    [SerializeField] private Slider slider_sound;
    [SerializeField] private Button btn_back;
    [SerializeField] private Text text_windowMode;
    [SerializeField] private Button btn_fullScreen;
    [SerializeField] private Text text_fullScreen;
    [SerializeField] private Button btn_window;
    [SerializeField] private Text text_window;

    [SerializeField] private Dropdown dpd_resolution;
    private Resolution[] resolutions;
    private List<string> resolutionsOptions = new List<string>();

    private void Reset()
    {
        text_volume = transform.Find("MainSetting/Volume/text_volume").GetComponent<Text>();
        slider_volume = transform.Find("MainSetting/Volume/slider_volume").GetComponent<Slider>();
        text_sound = transform.Find("MainSetting/Sound/text_sound").GetComponent<Text>();
        slider_sound = transform.Find("MainSetting/Sound/slider_sound").GetComponent<Slider>();
        btn_back = transform.Find("MainSetting/btn_back").GetComponent<Button>();
        text_windowMode = transform.Find("MainSetting/WindowMode/text_windowMode").GetComponent<Text>();
        btn_fullScreen = transform.Find("MainSetting/WindowMode/btn_fullScreen").GetComponent<Button>();
        text_fullScreen = transform.Find("MainSetting/WindowMode/btn_fullScreen/text_fullScreen").GetComponent<Text>();
        btn_window = transform.Find("MainSetting/WindowMode/btn_window").GetComponent<Button>();
        text_window = transform.Find("MainSetting/WindowMode/btn_window/text_window").GetComponent<Text>();
    }

#endregion

#region 生命周期

    /// <summary>
    /// 音量更新
    /// </summary> 
    /// <param name="param"></param>
    public override void OnLoaded(params object[] param)
    {
        base.OnLoaded(param);
        /// <summary>
        /// 初始化音量设置
        /// </summary>
        /// <returns></returns>
        AudioController music = AudioPlayer.GetAuidoController("Music");
        AudioController sound = AudioPlayer.GetAuidoController("Sound");

        slider_volume.value = music.Volume;
        slider_sound.value = sound.Volume;

        slider_volume.onValueChanged.AddListener((value) => 
        {
            music.Volume = value;
            gameController.MusicVolume = value; 
            // Debug.Log("music volume: " + music.Volume);
        });

        slider_sound.onValueChanged.AddListener((value) => 
        {
            sound.Volume = value;
            gameController.SoundVolume = value;
            // Debug.Log("sound volume: " + sound.Volume);
        });


        /// <summary>
        /// 分辨率设置
        /// </summary>
        dpd_resolution.ClearOptions();
        resolutions = Screen.resolutions;
        foreach (Resolution res in resolutions)
        {
            resolutionsOptions.Add(res.width + "x" + res.height);
        }
        dpd_resolution.AddOptions(resolutionsOptions);
        dpd_resolution.value = gameController.screenindex; // 设置 Dropdown 的值
        dpd_resolution.RefreshShownValue(); // 刷新显示
        dpd_resolution.onValueChanged.AddListener(UpdateResolution);



    }
#endregion
#region 方法
    /// <summary>
    /// 返回主菜单
    /// </summary>
    public void OnBtnBackClick()
    {
        Close();
    }

    /// <summary>
    /// 全屏 与窗口模式切换
    /// </summary> <summary>

    public void OnBtnFullScreenClick()
    {
        if (Screen.fullScreen) return;
        Screen.fullScreen = true;
        
    }

    public void OnBtnWindowClick()
    {
        if (!Screen.fullScreen) return;    
        Screen.fullScreen = false;
    }

    void UpdateResolution(int index)
    {
        gameController.screenindex = index;
        gameController.width = resolutions[index].width;
        gameController.height = resolutions[index].height;
        Resolution res = resolutions[index];
        Screen.SetResolution(res.width, res.height, Screen.fullScreen);

        LogUtil.Log("current resolution:{0},index:{1}",Screen.currentResolution.width +
         "x" + Screen.currentResolution.height, index);
    }

#endregion



}


